Page({
  data: {
    words:getApp().globalData.words,
    currentIndex: 0,
    currentWord: {e:'',c:''},
    favorites:{},
  },
  onLoad:function(){
    this.setData({currentWord:this.data.words[0]});
  },
  nextWord:function(){
    let newIndex=(this.data.currentIndex+1)%this.data.words.length;
    this.setData({
      currentIndex:newIndex,
      currentWord:this.data.words[newIndex]
    });
  },
  addFavorite:function(){
    const word=this.data.currentWord;
    const wordKey=word.e;
    let favorites=this.data.favorites;
    if(favorites[wordKey]){
      wx.showToast({
        title: '已收藏过了',
        icon:'success'
      });
    }else{
      favorites[wordKey]=word;
      wx.showToast({
        title: '收藏成功',
        icon:'success'
      });
      console.log(favorites);
    }
    this.setData({
      favorites,
    });
    const app = getApp();
    app.globalData.favorites = favorites;
    wx.setStorageSync('favorites', favorites);
    const mainPage = getCurrentPages().find(page => page.route === 'pages/main1/main1')
    if(mainPage){
      mainPage.setData({
        reviewlen:Object.keys(app.globalData.favorites).length
      })
    }
  },
})